package mobi.bantu.web;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import mobi.bantu.bean.SearchParam;
import mobi.bantu.bean.Page;
import mobi.bantu.bean.Result;
import mobi.bantu.bean.SecUser;
import mobi.bantu.service.SecUserService;
import mobi.bantu.utils.Md5SaltTool;

/**
 * Created by qiao on 2017/5/3.
 */
@Controller
@RequestMapping("secUser")
public class SecUserController {

    @Autowired
    private SecUserService secUserService;

    @RequestMapping("list")
    public String list() {
        return "sec/list";
    }

    @RequestMapping("list-data")
    @ResponseBody
    public Page<SecUser> listData(SearchParam sp) {
        Map<String, Object> param = sp.getSp();
        return secUserService.page(param);
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam(value = "id", required = true) Long id) {
        int rs = secUserService.delete(id);
        return rs > 0 ? Result.success() : Result.byMsg("失败");
    }

    @RequestMapping("form")
    public String toForm() {
        return "sec/form";
    }

    @RequestMapping("toUpdate")
    public String toForm(Long id, Model model) {
        model.addAttribute("info", secUserService.getById(id));
        return "sec/update";
    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(SecUser user) {
        if (user == null || user.getUsername() == null || user.getPassword() == null)
            return Result.byMsg("参数错误");
        if (secUserService.getByname(user.getUsername()) != null)
            return Result.byMsg("用户名已存在");
        user.setCreateTime(new Date());
        try {
            user.setPassword(Md5SaltTool.getEncryptedPwd(user.getPassword()));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.byMsg("新增失败");
        }
        int rs = secUserService.save(user);
        return rs > 0 ? Result.success() : Result.byMsg("新增失败");
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(SecUser user) {
        SecUser old = null;
        if (user.getId() == null || (old = secUserService.getById(user.getId())) == null) {
            return Result.byMsg("参数错误");
        }
        if (StringUtils.isNotBlank(user.getPassword())
                && !old.getPassword().equals(user.getPassword())) {
            try {
                user.setPassword(Md5SaltTool.getEncryptedPwd(user.getPassword()));
            } catch (Exception e) {
                e.printStackTrace();
                return Result.byMsg("修改失败");
            }
        }
        int rs = secUserService.update(user);
        return rs > 0 ? Result.success() : Result.byMsg("修改失败");
    }
}
